using RadioTimer.ViewModels;
using RadioTimer.Services;

namespace RadioTimer.Views
{
    /// <summary>
    /// 设置页面的代码后置类
    /// 继承自ContentPage，作为语音报时App的设置页面
    /// 提供24小时报时设置、批量操作和个性化配置功能
    /// 通过数据绑定与SettingsPageViewModel进行交互
    /// </summary>
    public partial class SettingsPage : ContentPage
    {
        /// <summary>
        /// 初始化SettingsPage实例
        /// 通过依赖注入获取ViewModel实例，并设置为数据绑定上下文
        /// </summary>
        /// <param name="viewModel">设置页面的视图模型实例</param>
        public SettingsPage(SettingsPageViewModel viewModel)
        {
            InitializeComponent();
            BindingContext = viewModel;
            
        }
        
        /// <summary>
        /// 全局音量滑块值变更事件处理
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private async void OnGlobalVolumeChanged(object sender, ValueChangedEventArgs e)
        {
            if (BindingContext is SettingsPageViewModel viewModel)
            {
                await viewModel.SetGlobalVolumeAsync(e.NewValue);
            }
        }
        
        /// <summary>
        /// 全局重复次数滑块值变更事件处理
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">事件参数</param>
        private async void OnGlobalRepeatChanged(object sender, ValueChangedEventArgs e)
        {
            if (BindingContext is SettingsPageViewModel viewModel)
            {
                await viewModel.SetGlobalRepeatCountAsync((int)Math.Round(e.NewValue));
            }
        }
        
        /// <summary>
        /// 页面出现时加载设置
        /// </summary>
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            
            if (BindingContext is SettingsPageViewModel viewModel)
            {
                // 初始化24小时设置列表
                viewModel.InitializeHourlySettings();
                await viewModel.LoadSettingsAsync();
            }
        }
    }
}